<?php
class App_CategoryGl extends Data_Base
{
    public static function listByTarget ($targetType, $targetNid)
    {
        $query = DB::select('c.name', 'gl.*')
            ->from(array('tiyu_category', 'c'))
            ->join(array('tiyu_categorygl', 'gl'), 'LEFT')
            ->on('gl.categoryId', '=', 'c.id')
            ->where('gl.targetType', '=', $targetType)
            ->where('gl.targetNid', '=', $targetNid)
            ->execute();

        return $query->as_array();
    }


    public static function setTarget ($uid, $targetType, $targetNid, $cids)
    {
        if (empty($cids))
            $currCids = array(-1);
        else
            $currCids = $cids;

        // 删除旧的cid
        $query = DB::delete('tiyu_categorygl')
            ->where('targetType', '=', $targetType)
            ->where('targetNid', '=', $targetNid)
            ->where('categoryId', 'not in', $currCids)
            ->execute();

        foreach ($cids as $cid)
        {
            $id = DB::select('id')
                ->from('tiyu_categorygl')
                ->where('targetType', '=', $targetType)
                ->where('targetNid', '=', $targetNid)
                ->where('categoryId', '=', $cid)
                ->execute()
                ->get('id');

            if ($id > 0)
            {
                //
            }
            else
            {
                $sort = DB::select(DB::expr('max("sort")', 'max'))
                    ->from('tiyu_categorygl')
                    ->where('categoryId', '=', $cid)
                    ->limit(1)
                    ->execute()
                    ->get('max');

                $sort = $sort + 1;
                $row  = array(
                    'categoryId' => $cid,
                    'targetType' => $targetType,
                    'targetNid'  => $targetNid,
                    'uid'        => $uid,
                    'sort'       => $sort,
                    'created'    => DB::expr('unix_timestamp(now())'),
                    'changed'    => DB::expr('unix_timestamp(now())'),
                );

                DB::insert('tiyu_categorygl')
                    ->columns(array_keys($row))
                    ->values (array_values($row))
                    ->execute();
            }

        } // foreach
    }

    public static function items ($catid, $offset, $limit)
    {
        $user  = user::currentUser();
        $query = DB::select('catgl.*', 
                array('c.name', 'catName'), 
                array('act.name', 'glName'),
                array('act.thumb', 'glImage')
            )
            ->from(array('tiyu_categorygl', 'catgl'))

            ->join(array('tiyu_category', 'c'), 'LEFT')
            ->on('catgl.categoryId', '=', 'c.id')

            ->join(array('mod_activity', 'act'), 'LEFT')
            ->on('catgl.targetNid', '=', 'act.id')

            ->where(DB::expr('catgl.categoryId'), '=', $catid)
            ->order_by('sort', 'asc');

        if ($offset > 0)
            $query->offset($offset);

        if ($limit > 0)
            $query->limit($limit);

        $query = $query->execute()->as_array();
        foreach ($query as & $rs)
        {
            $rs['countsLike'] = 0;
            $rs['isLiked'] = 0;
        }
	
        return $query;
    }

    public static function info ($nid)
    {
        $detail = self::item('tiyu_gonglue', 'id', $nid);
        if (empty($detail))
            return array();

        return array(
            'name' => $detail['name'],
            'id'   => $detail['id'],
            'image' => $detail['image'],
            'url'    => 'http://'.$_SERVER['HTTP_HOST'].'/m/gonglue/detail/'.$nid,
        );
    }



    public static function detail ($nid)
    {
        $detail = self::item('tiyu_categorygl', 'id', $nid);
        if (empty($detail))
            return array();

        return $detail;
        $goodsList = unserialize($detail['serializeGoodsList']);
        $index = 1;
        foreach ($goodsList as & $goods)
        {
            $goods['goodsInfo'] = App_Goods::info ($goods['goodsId']);
            $goods['indexNo']   = $index++;
        }

        $detail['serializeGoodsList'] = $goodsList;

        return $detail;
    }
}
